#include "stdafx.h"
#include "VideoAction.h"
#include "videocapturetask.h"
#include "videoencodetask.h"
VideoAction::VideoAction(const std::string &camera_name, const std::string &info, unsigned int bps /*= 300*/, unsigned int fps /*= 10*/, VideoCapureOut *pVideoCapureOut /*= NULL*/)
{
	m_ikbps = bps;
	m_ifps = fps;

	m_strCameraName = new std::string(camera_name);
	m_strCameraInfo = new std::string(info);

	m_oVideoCaptureTask = NULL;
	m_oVideoEncodeTask = NULL;
	m_oVideoCapureOut = pVideoCapureOut;
}

VideoAction::~VideoAction()
{
	delete m_strCameraName;
	delete m_strCameraInfo;
}
std::list<std::string>  VideoAction::get_camera_list()
{
	std::list<std::string> sList;
    VideoCaptureTask ovideoCaptureTask;
    ovideoCaptureTask.get_camera_list(sList);
    return sList;
}

std::list<std::string> VideoAction::get_camera_info_list(const std::string &camera_name)
{
	std::list<std::string> listInfo;
    VideoCaptureTask ovideoCaptureTask;
    ovideoCaptureTask.get_camera_info_list(camera_name,listInfo);
    return listInfo;
}

std::string VideoAction::getX264Header()
{
	if (m_oVideoEncodeTask)
		return m_oVideoEncodeTask->getX264Header();
	return "";
}

std::string VideoAction::getX264SeiInfo()
{
	if (m_oVideoEncodeTask)
		return m_oVideoEncodeTask->getX264SeiInfo();
	return "";
}

float VideoAction::getFrameDuration()
{
	if (m_oVideoEncodeTask)
		return m_oVideoEncodeTask->getFrameDuration();
	return 0.0;
}

bool VideoAction::OnInit()
{
	if (m_strCameraName->empty() || m_strCameraInfo->empty())
		return false;

	if (m_oVideoCaptureTask == NULL)
		m_oVideoCaptureTask = new VideoCaptureTask;

	if (m_oVideoEncodeTask == NULL)
		m_oVideoEncodeTask = new VideoEncodeTask;

	m_oVideoCaptureTask->set_capture(m_oVideoCapureOut);

	if (m_oVideoCaptureTask->Init(*m_strCameraName, *m_strCameraInfo, m_oVideoEncodeTask) == false)//
		return false;

	if (m_oVideoEncodeTask->init(m_oVideoCaptureTask->Width(), m_oVideoCaptureTask->Hight(), m_ikbps, m_ifps) == false)    //
		return false;

	return true;
}

bool VideoAction::OnStart()
{
	if (m_oVideoCaptureTask)
		m_oVideoCaptureTask->activate();//

	if (m_oVideoEncodeTask)
		m_oVideoEncodeTask->activate();//

	return true;
}

bool VideoAction::OnStop()
{
	if (m_oVideoCaptureTask)
		m_oVideoCaptureTask->close();

	if (m_oVideoEncodeTask)
		m_oVideoEncodeTask->close();

	if (m_oVideoCaptureTask)
		delete m_oVideoCaptureTask;
	m_oVideoCaptureTask = NULL;

	if (m_oVideoEncodeTask)
		delete m_oVideoEncodeTask;
	m_oVideoEncodeTask = NULL;

	return true;
}

Receive_handler * VideoAction::getCaptureHandle()
{
	return m_oVideoCaptureTask;
}
